package com.optimatch.ui.controler;

import java.util.Locale;

import com.optimatch.ui.controler.actions.Action;
import com.optimatch.ui.controler.actions.AddContrainteDateAction;
import com.optimatch.ui.controler.actions.AddContraintePeriodeAction;
import com.optimatch.ui.controler.actions.AddProblemFromFile;
import com.optimatch.ui.controler.actions.ListAction;
import com.optimatch.ui.controler.actions.ResoudreAction;
import com.optimatch.ui.controler.listeners.ComboBoxEquipesResultat;
import com.optimatch.ui.controler.listeners.ComboBoxJourneeResultat;
import com.optimatch.ui.controler.localization.LanguageManager;
import com.optimatch.ui.controler.observers.ModelObserver;
import com.optimatch.ui.model.Model;
import com.optimatch.ui.view.View;
import com.optimatch.ui.view.customComponents.pages.creation_equipes.EquipesPanelAddListener;
import com.optimatch.ui.view.customComponents.pages.creation_equipes.EquipesPanelRemoveListener;


public class Controler {
	
	private View view;
	private Model model;
	private ListAction actions;
	private Action contrainteDateAction;
	private Action contraintePeriodeAction;
	private Action problemFromFile;
	private Action resoudreAction;

	public Controler(Model model) {
		setLocale();
		this.model = model;
		setLocale();
		view = new View();
		initActions();
		initObservers();
		initListeners();
		init();
		launch();
	}

	// Initializers

	private void initActions() {
		actions = new ListAction();
		contrainteDateAction = new AddContrainteDateAction(model, view, actions);
		contraintePeriodeAction = new AddContraintePeriodeAction(model, view, actions);
		problemFromFile = new AddProblemFromFile(model, view, actions);
		resoudreAction = new ResoudreAction(model,view,actions,this);
		
		view.getTabs().getContraintesPanel().getFixerDatePanel().getAjouterMatch()
							.setAction(contrainteDateAction);
		view.getTabs().getContraintesPanel().getFixerPeriodePanel().getAjouterMatch()
							.setAction(contraintePeriodeAction);
		
		view.getMenu().getLoadFile().setAction(problemFromFile);
		view.getMenu().reloadJMenuItemLoad(); //je ne comprends pas pourquoi, mais ca regle le probleme
		
		view.getTabs().getResoudrePanel().getResoudreButton().setAction(resoudreAction);
	}

	private void initObservers() {
		ModelObserver observer = new ModelObserver(this);
		model.addObserver(view.getTabs().getContraintesPanel().getFixerDatePanel());
		model.addObserver(view.getTabs().getContraintesPanel().getFixerPeriodePanel());
		model.addObserver(observer);
		model.notifyObservers();
	}
	
	private void initListeners() {
		view.getTabs().getResoudrePanel().getResultatParEquipe().getChoixEquipe().addActionListener(new ComboBoxEquipesResultat(this));
		view.getTabs().getResoudrePanel().getResultatParJournee().getChoixJournee().addActionListener(new ComboBoxJourneeResultat(this));
		view.getTabs().getEquipesPanel().getBut_create().addActionListener(new EquipesPanelAddListener(this));
		view.getTabs().getEquipesPanel().getBut_remove().addActionListener(new EquipesPanelRemoveListener(this));
	}
	
	private void init() {
		view.getTabs().getEquipesPanel().getField_ville().setDico(model.getProbleme().getDico());
	}
	
	// Native Getters

	public View getView() {
		return view;
	}

	public Model getModel() {
		return model;
	}

	public ListAction getActions() {
		return actions;
	}
	
	private void setLocale() {
		Locale currentLocale = new Locale("fr","FR");
//		Locale currentLocale = new Locale("en","US");
		LanguageManager.setBundle(currentLocale);
	}

	// Launch
	
	public void launch() {
		view.display();
	}

}
